package cn.kgc.fcm.dsbank.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee extends BaseEntity  implements UserDetails{


    private Integer empId;              //主键id
    @NotNull(message = "姓名不能为空")
    @NotBlank(message = "没看到您添加的数据")
    private String empName;             //员工姓名
    @NotNull(message = "用户名不能为空")
    @Length(min = 4, max = 30, message = "用户名只能在4~30位之间")
    private String empUsername;         //员工用户名
    private String empPassword;         //员工密码

    @NotNull(message = "身份证不能为空")
    @Pattern(regexp = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$",message = "身份证号不合法")
    private String empAD;               //员工身份证

//    @Pattern(regexp = "^d{4}$", message = "电话号码格式不正确")
    private String empTelNo;            //员工办公电话

    private List<Role> roleList;   //封装角色


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        Set<Authority> authoritySet = new HashSet<>();

        for (Role role : this.getRoleList()) {
            for (Authority authority : role.getAuthorityList()) {
                authoritySet.add(authority);
            }
        }

        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();

        for (Authority authority : authoritySet) {
            grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_"+authority.getAuthorityName()));
        }

        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return empPassword;
    }

    @Override
    public String getUsername() {
        return empUsername;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {

//        if(this.getStatus() == 1){
//            return true;
//        }else {
//            return false;
//        }
        return this.getStatus() == 1 ? true:false;
    }
}
